package com.CoreryBlack.controller;

import com.CoreryBlack.anno.Log;
import com.CoreryBlack.po.Dept;
import com.CoreryBlack.po.Result;
import com.CoreryBlack.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Description DeptController
 * @Author CoreryBlack
 * @Date 2024-09-20
 */
@Slf4j
@RestController
@RequestMapping("/depts") //当前控制器所有方法统一请求路径前缀
public class DeptController {

    //各层之间依赖接口，而不是实现类
    @Autowired //依赖注入
    private DeptService deptService;

    /**
     * 查询部门列表
     * @return
     */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式
    @GetMapping //只支持GET方式请求
    public Result list() throws Exception{
        List<Dept> deptList = deptService.queryDept();
        //3、响应回数据
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     * @param request
     * @param id
     * @return
     */
    @Log
    @DeleteMapping
    public Result deleteById(HttpServletRequest request, Integer id){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result saveDept(@RequestBody Dept dept){
        deptService.saveDept(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门，用于回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @GetMapping("/{v1}/{v2}")
    public Result test(@PathVariable String v1, @PathVariable String v2){
        return Result.success();
    }

    /**
     * 根据id修改部门
     * @param dept
     * @return
     */
    @Log
    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

}